package io.framework.modules.noise.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.framework.common.constant.Constant;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.modules.noise.dao.NoiseFunctionalZoneDao;
import io.framework.modules.noise.dto.NoiseFunctionalZoneDTO;
import io.framework.modules.noise.entity.NoiseFunctionalZoneEntity;
import io.framework.modules.noise.service.NoiseFunctionalZoneService;
import io.framework.modules.noise.vo.NoiseFunctionalZoneVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能区噪声
 *
 * @author Gujq email
 * @since 1.0.0 2020-06-02
 */
@Service
public class NoiseFunctionalZoneServiceImpl extends CrudServiceImpl<NoiseFunctionalZoneDao, NoiseFunctionalZoneEntity, NoiseFunctionalZoneDTO> implements NoiseFunctionalZoneService {

    @Override
    public QueryWrapper<NoiseFunctionalZoneEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<NoiseFunctionalZoneEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    @Override
    public PageData<NoiseFunctionalZoneDTO> page(Map<String, Object> params) {

        IPage<NoiseFunctionalZoneEntity> pages = getPage(params, Constant.CREATE_DATE, false);

        List<NoiseFunctionalZoneEntity> list = baseDao.getList(params);

        return getPageData(list, pages.getTotal(), NoiseFunctionalZoneDTO.class);
    }

    @Override
    public PageData<NoiseFunctionalZoneVo> pageList(Map<String, Object> params) {
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
//        String orderField = jsonObject.getString("orderField");
//        String order = jsonObject.getString("order");
        String time = jsonObject.getString("time");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        QueryWrapper<NoiseFunctionalZoneEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(time), "date_format(month,'%Y')", time);
        Page<NoiseFunctionalZoneEntity> p = new Page<>(page, limit);
        queryWrapper.select("station_id");
        queryWrapper.groupBy("station_id");
        IPage<NoiseFunctionalZoneEntity> ip = baseDao.selectPage(p, queryWrapper);
        List<NoiseFunctionalZoneVo> vos = new ArrayList<>();
        if (ip.getTotal() > 0) {
            List<Long> stationIds = new ArrayList<>();
            ip.getRecords().stream().forEach(r -> {
                stationIds.add(r.getStationId());
            });
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(StringUtils.isNotBlank(time), "date_format(month,'%Y')", time);
            queryWrapper.in("station_id", stationIds);
            List<NoiseFunctionalZoneEntity> list = baseDao.selectList(queryWrapper);
            stationIds.stream().forEach(stationId -> {
                NoiseFunctionalZoneVo vo = new NoiseFunctionalZoneVo();
                vo.setStationId(stationId);
                List<NoiseFunctionalZoneEntity> list2 = list.stream().filter(r -> r.getStationId().equals(stationId)).collect(Collectors.toList());
                if (!list2.isEmpty() && list2.size() > 0) {
                    //设置基础数据
                    NoiseFunctionalZoneEntity nze = list2.get(0);
                    vo.setClassify(nze.getClassify());
                    vo.setLdStandard(nze.getLdStandard());
                    vo.setLnStandard(nze.getLnStandard());
                    vo.setMonth(nze.getMonth());
                    vo.setStationName(nze.getStationName());
                    vo.setRemark(nze.getRemark());
                    //设置季度数据
                    Class<?> voClass = vo.getClass();
                    list2.stream().forEach(r -> {
                        try {
                            Field ld = voClass.getDeclaredField("ld" + r.getQuarter());
                            ld.setAccessible(true);
                            ld.set(vo, r.getLd());
                            Field ln = voClass.getDeclaredField("ln" + r.getQuarter());
                            ln.setAccessible(true);
                            ln.set(vo, r.getLn());
                            Field ldPass = voClass.getDeclaredField("ldPass" + r.getQuarter());
                            ldPass.setAccessible(true);
                            ldPass.set(vo, r.getLdPass());
                            Field lnPass = voClass.getDeclaredField("lnPass" + r.getQuarter());
                            lnPass.setAccessible(true);
                            lnPass.set(vo, r.getLnPass());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                }
                vos.add(vo);
            });
        }
        return new PageData<NoiseFunctionalZoneVo>(vos, ip.getTotal());
    }

    @Override
    public Boolean checkData(String year) {
        Boolean flag = false;
        List<NoiseFunctionalZoneEntity> list = baseDao.checkData(year);
        if (CollectionUtils.isNotEmpty(list)) {
            flag = true;
        }
        return flag;
    }

    @Override
    public int deleteInfoByDate(Map<String, Object> params) {

        return baseDao.deleteInfoByDate(params);
    }

    @Override
    public List<Map<String, Object>> pass(Map<String, Object> params) {

        return baseDao.pass(params);
    }

    @Override
    public List<Map<String, Object>> passNum(Map<String, Object> params) {

        return baseDao.passNum(params);
    }

    @Override
    public List<Map<String, Object>> passRate(Map<String, Object> params) {

        return baseDao.passRate(params);
    }

    @Override
    public List<Map<String, Object>> classifyInfo(Map<String, Object> params) {

        return baseDao.classifyInfo(params);
    }


    @Override
    public List<Map<String, Object>> getNoiseInfo(Map<String, Object> params) {

        return baseDao.getNoiseInfo(params);
    }

    @Override
    public Map<String, Object> getNewCreateDate() {

        return baseDao.getNewCreateDate();
    }
}
